package org.spring.springboot.config;

import java.io.IOException;
import java.io.OutputStream;

import org.springframework.http.HttpOutputMessage;
import org.springframework.http.converter.HttpMessageNotWritableException;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class MFastJsonHttpMessageConverter extends com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter {  
    @Override  
    protected void writeInternal(Object obj, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {  
        if (obj instanceof JSONPObject) {  
            JSONPObject jsonp = (JSONPObject) obj;  
            OutputStream out = outputMessage.getBody();  
            String text ;
            if(null==jsonp.getFunction())
            	  text = JSON.toJSONString(jsonp.getJson(),getFeatures());
            else
            	  text = jsonp.getFunction() + "(" + JSON.toJSONString(jsonp.getJson(),getFeatures()) + ")";  
            byte[] bytes = text.getBytes(getCharset());  
            out.write(bytes);  
        } else {  
            super.writeInternal(obj, outputMessage);  
        }  
    }   
} 